package com.sign2024.sign2024project.hetong.service.impl;

import com.alibaba.excel.EasyExcel;
import com.sign2024.sign2024project.hetong.entry.PriceLinkPact;
import com.sign2024.sign2024project.hetong.entry.SignMessage;
import com.sign2024.sign2024project.hetong.mapper.SignMessageMapper;
import com.sign2024.sign2024project.hetong.service.SignMessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * @author yujie
 * @data 2024/12/3 8:30
 */
@Service
public class SignMessageServiceImpl implements SignMessageService {

    @Resource
    private SignMessageMapper signMessageMapper;

    @Override
    public void insertSignMessage(List<SignMessage> list) {
        if (list.isEmpty()) {
            throw new RuntimeException("分标信息导入失败");
        } else {
            for (SignMessage sign : list) {
                signMessageMapper.insertSignMessage(sign);
            }
        }
    }

    @Override
    public void insertPriceLinkPact(List<PriceLinkPact> list) {
        if (list.isEmpty()) {
            throw new RuntimeException("设计价格联动合同表导入失败");
        } else {
            for (PriceLinkPact linkPact : list) {
                signMessageMapper.insertPriceLinkPact(linkPact);
            }
        }
    }

    @Override
    public void testInport() throws Exception {
        this.importAllInfo();
    }

    public void importAllInfo() throws Exception {

        String filePath = "C:\\Users\\86182\\Desktop\\合同\\人工录入表.xlsx";
        InputStream inputStream = new FileInputStream(filePath);
        List<SignMessage> signList =
                EasyExcel.read(inputStream).head(SignMessage.class).sheet("分标信息").doReadSync();
        signList.forEach(System.out::println);

        InputStream inputStream2 = new FileInputStream(filePath);
        List<PriceLinkPact> linkList =
                EasyExcel.read(inputStream2).head(PriceLinkPact.class).sheet("设计价格联动合同表").doReadSync();
        linkList.forEach(System.out::println);

        if (signList.isEmpty()) {
            throw new RuntimeException("分标信息导入失败");
        } else {
            for (SignMessage sign : signList) {
                signMessageMapper.insertSignMessage(sign);
            }
        }

        if (linkList.isEmpty()) {
            throw new RuntimeException("设计价格联动合同表导入失败");
        } else {
            for (PriceLinkPact linkPact : linkList) {
                signMessageMapper.insertPriceLinkPact(linkPact);
            }
        }
    }
}
